package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Insurance;
import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.impl.InsuranceQueryObject;
import cn.wolfcode.crm.service.ICarInformationService;
import cn.wolfcode.crm.service.IInsuranceService;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("insurance")
public class InsuranceController {

    @Autowired
    private IInsuranceService insuranceService;
    @Autowired
    private ICarInformationService carInformationService;
    @Autowired
    private IOrganizationService organizationService;

    /**
     * 保单投保
     */
    @RequestMapping("add")
    @RequiresPermissions(value = {"insurance:view", "保单页面"}, logical = Logical.OR)
    public String view(Map map) {
        map.put("cars", carInformationService.getAll());
        map.put("organizations", organizationService.getAll());
        return "insurance/insurance";
    }

    /**
     * 页面跳转
     */
    @RequestMapping("tempStor")
    @RequiresPermissions(value = {"insurance:tempStor", "暂存单"}, logical = Logical.OR)
    public String tempStor(Map map) {
        return "insurance/tempStor";
    }

    @RequestMapping("correct")
    @RequiresPermissions(value = {"insurance:correct", "批改单"}, logical = Logical.OR)
    public String correct(Map map) {
        return "insurance/correct";
    }

    @RequestMapping("audit")
    @RequiresPermissions(value = {"insurance:audit", "待审单"}, logical = Logical.OR)
    public String audit(Map map) {
        return "insurance/audit";
    }

    /**
     * 查询历史保单
     */
    @RequestMapping("history")
    @RequiresPermissions(value = {"insurance:history", "单据历史记录"}, logical = Logical.OR)
    public String history(Map map) {
        return "insurance/history";
    }


    /**
     * 删除保单
     */
    @RequestMapping("payProcess")
    @RequiresPermissions(value = {"insurance:payProcess", "缴费处理记录"}, logical = Logical.OR)
    public String payProcess(Map map) {
        return "seeCostSheet/payProcess";
    }

    @RequestMapping("chargeQuery")
    @RequiresPermissions(value = {"insurance:chargeQuery", "收费处理记录"}, logical = Logical.OR)
    public String chargeQuery(Map map) {
        return "seeCostSheet/chargeQuery";
    }

    @RequestMapping("refuse")
    @RequiresPermissions(value = {"insurance:refuse", "单据拒保记录"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult refuse(Long id) {
        try {
            insuranceService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }

    /**
     * 提交至未审核页面
     */
    @RequestMapping("submitAudit")
    @RequiresPermissions(value = {"insurance:refuse", "单据提审"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult submitAudit(Long id) {
        try {
            insuranceService.submitAudit(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }

    /**
     * 待审核页面退回批改单
     */
    @RequestMapping("returnApplication")
    @RequiresPermissions(value = {"insurance:returnApplication", "退回保单"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult returnApplication(Long id) {
        try {
            insuranceService.returnApplication(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }

    /**
     * 通过审核
     */
    @RequestMapping("passApplication")
    @RequiresPermissions(value = {"insurance:returnApplication", "单据拒保记录"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult passApplication(Long id) {
        try {
            insuranceService.passApplication(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }

    @RequestMapping("getProductById")
    @ResponseBody
    public Product getProductById(Long id) {
        return insuranceService.getProductById(id);
    }

    /**
     * 保存保单
     */
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"insurance:save", "保存员工"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Insurance insurance) {
        try {
            insuranceService.saveOrUpdate(insurance);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }

    /**
     * 查询保单列表
     */
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(InsuranceQueryObject qo) {
        return insuranceService.query(qo);
    }


    /**
     * 提交缴费和缴费方式
     */
    @RequestMapping("payInsurance")
    @ResponseBody
    public JSONResult payInsurance(Insurance insurance){
        try {
            insuranceService.payInsurance(insurance);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "保存失败");
        }
        return new JSONResult(true, "保存成功");
    }
    /**
     * 查询单条保单
     */
    @RequestMapping("getByIdAndState")
    @ResponseBody
    public Insurance getByIdAndState(Long id, int state) {
        return insuranceService.getByIdAndState(id, state);
    }


}
